package com.hq.ims.data.service.other;

import com.hq.ims.data.dao.StatisticsMapper;
import com.hq.ims.data.entity.BusiIndustryAssign;
import com.hq.ims.data.entity.BusiInfo;
import com.hq.ims.data.entityenum.EnumReceiveStatus;
import com.hq.ims.data.service.BaseBusiIndustryAssignService;
import com.hq.ims.data.service.BaseBusiInfoService;
import com.hq.ims.data.vo.statistics.PublishStatisticsSearchVo;
import com.hq.ims.data.vo.statistics.PublishStatisticsVo;
import com.hq.ims.data.vo.statistics.TrackStatisticsVo;
import com.hq.ims.data.vo.statistics.statisticsEnum.EnumDimension;
import com.hq.utils.util.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yinzhangxiao
 * @date 2024/11/1 14:35
 * @ClassName BusiInfoStatisticsService
 **/
@Service
public class BusiInfoStatisticsService {

    @Resource
    private StatisticsMapper statisticsMapper;
    @Resource
    private BaseBusiInfoService baseBusiInfoService;

    @Resource
    private BaseBusiIndustryAssignService baseBusiIndustryAssignService;

    public PublishStatisticsVo pubulishStatistics(PublishStatisticsSearchVo searchVo) {
        PublishStatisticsVo publishStatisticsVo = new PublishStatisticsVo();
        long publishCount = 0;
        long receiveCount = 0;
        long refulseCount = 0;
        //总的发布数量
        List<BusiInfo> busiInfoList = baseBusiInfoService.lambdaQuery()
                .select(BusiInfo::getBusiInfoId)
                .eq(StringUtils.isNotEmpty(searchVo.getOrgId()), BusiInfo::getOrgId, searchVo.getOrgId())
                .in(BusiInfo::getInfoType, searchVo.getInfoTypeList())
                .list();
        if (CollectionUtils.isNotEmpty(busiInfoList)) {
            publishCount = busiInfoList.size();
            List<String> busiInfoIds = busiInfoList.stream().map(BusiInfo::getBusiInfoId).collect(Collectors.toList());
            //总的被接收数量
            receiveCount = baseBusiIndustryAssignService.lambdaQuery()
                    .eq(BusiIndustryAssign::getReceiveStatus, EnumReceiveStatus.Receive)
                    .in(BusiIndustryAssign::getBusiInfoId, busiInfoIds)
                    .count();
            //总的被拒绝数量
            refulseCount = baseBusiIndustryAssignService.lambdaQuery()
                    .eq(BusiIndustryAssign::getReceiveStatus, EnumReceiveStatus.Rejection)
                    .in(BusiIndustryAssign::getBusiInfoId, busiInfoIds)
                    .count();
        }
        publishStatisticsVo.setPublishCount(publishCount);
        publishStatisticsVo.setReceiveCount(receiveCount);
        publishStatisticsVo.setRefulseCount(refulseCount);
        //查询统计数据
        List<PublishStatisticsVo> statisticsList = statisticsMapper.pubulishStatistics(searchVo);
        if (searchVo.getDimension().getCode() == 2) {
            statisticsList.forEach(item -> item.setCountDate(item.getCountDate().substring(5)));
        }
        List<PublishStatisticsVo> resultList = new ArrayList<>();
        //日期间隔空值补0
        int time;
        switch (searchVo.getDimension()) {
            case Year:
                if (CollectionUtils.isNotEmpty(statisticsList)) {
                    time = LocalDate.now().getYear() - Integer.parseInt(statisticsList.get(0).getCountDate()); //显示最早年份到现在年份
                } else {
                    time = 0;//显示1年以内
                }
                break;
            case Month:
                time = 11;
                break;
            case Day:
                time = 29;
                break;
            default:
                time = 0;
                break;
        }
        //根据统计维度获取需统计的日期集合组装统计数据
        List<String> dateList = this.getDateList(time, searchVo.getDimension());
        dateList.forEach(date -> {
            boolean flag = true;
            PublishStatisticsVo item = null;
            for (PublishStatisticsVo statisticsVo : statisticsList) {
                if (statisticsVo.getCountDate().equals(date)) {
                    flag = false;
                    item = statisticsVo;
                }
            }
            if (flag) {
                item = new PublishStatisticsVo();
                item.setCountDate(date);
                item.setPublishCount(0);
                item.setRefulseCount(0);
                item.setReceiveCount(0);
            }
            resultList.add(item);
        });
        publishStatisticsVo.setStatisticsList(resultList);
        return publishStatisticsVo;
    }

    public TrackStatisticsVo trackStatistics(String orgId) {
        return statisticsMapper.trackStatistics(orgId);
    }

    private List<String> getDateList(int time, EnumDimension dimension) {
        List<String> dateList = new ArrayList<>(time + 1);
        if (dimension.getCode() == EnumDimension.Month.getCode()) {
            for (int i = time; i >= 0; i--) {
                LocalDate localDate = LocalDate.now().minusMonths(i);
                dateList.add(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM")));
            }
        } else if (dimension.getCode() == EnumDimension.Day.getCode()) {
            for (int i = time; i >= 0; i--) {
                LocalDate localDate = LocalDate.now().minusDays(i);
                dateList.add(localDate.format(DateTimeFormatter.ofPattern("MM-dd")));
            }
        } else if (dimension.getCode() == EnumDimension.Year.getCode()) {
            for (int i = time; i >= 0; i--) {
                LocalDate localDate = LocalDate.now().minusYears(i);
                dateList.add(localDate.format(DateTimeFormatter.ofPattern("yyyy")));
            }
        }
        return dateList;
    }
}
